package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    ItemDescMapper descMapper;

    @Override
    public ProductDetailDto getProductDetail(Long id) {
        Item item = itemMapper.selectByPrimaryKey(id);
        ItemDesc itemDesc = descMapper.selectByPrimaryKey(id);
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        return productDetailDto;
    }
    /**
     * 查询所有商品（分页）
     * @return
     */
    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getSize());
        List<Item> items = null;
        if (pageInfo.getSort().equals("1")) {
            //升序
            String orderDir = "ASC";
            String orderCol = "price";
            items = itemMapper.selectItemFront(null, orderCol, orderDir, pageInfo.getPriceGt(), pageInfo.getPriceLte());
        } else if (pageInfo.getSort().equals("-1")) {
            //降序
            String orderDir = "DESC";
            String orderCol = "price";
            items = itemMapper.selectItemFront(null, orderCol, orderDir, pageInfo.getPriceGt(), pageInfo.getPriceLte());
        } else if (pageInfo.getPriceGt() != null || pageInfo.getPriceLte() != null) {
            //筛选出价格区间的商品，按照默认排序进行排序
            String orderDir = "DESC";
            String orderCol = "created";
            items = itemMapper.selectItemFront(null, orderCol, orderDir, pageInfo.getPriceGt(), pageInfo.getPriceLte());
        }else if (pageInfo.getCid()!=null){
            //按照默认排序，显示所有的商品信息
            Example example = new Example(Item.class);
            example.createCriteria()
                    .andEqualTo("status", 1)
                    .andEqualTo("cid", pageInfo.getCid());
            items = itemMapper.selectByExample(example);
        } else {
            //按照默认排序，显示所有的商品信息
            Example example = new Example(Item.class);
            example.createCriteria().andEqualTo("status", 1);
            items = itemMapper.selectByExample(example);
        }
        PageInfo<Item> itemsPageInfo = new PageInfo<>(items);
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setData(productDtos);
        allProductResponse.setTotal(itemsPageInfo.getTotal());
        return allProductResponse;
    }
    /**
     * 获取推荐的商品板块
     * @return
     */
    @Override
    public Set<PanelDto> getRecommendGoods() {

        List<Panel> panels = panelMapper.selectAll();
        Set<PanelDto> panelDtos = new HashSet();

        for (Panel panel : panels) {
            if (panel.getId()== GlobalConstants.RECOMMEND_PANEL_ID) {
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(GlobalConstants.RECOMMEND_PANEL_ID);
                panel.setPanelContentItems(panelContentItems);

                PanelDto panelDto = contentConverter.panel2Dto(panel);
                panelDtos.add(panelDto);
                break;
            }
        }
        return panelDtos;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}
